using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
	/// <summary> 
	///Author: nnamthach@gmail.com 
	/// <summary>
    public class TheoDoiSXDataAccess : Connection
    {
		#region Local Variable
        public TheoDoiSXDataAccess(string type, string connectString, int timeout = 0) : base(type, connectString, timeout) { }
        public TheoDoiSXDataAccess(Connection connection) : base(connection) { }
		#endregion Local Variable
		
		#region Method        
		public DataTable GetShortData(Int64 IdSX, String XuongId, List<DTO.FilterParameter> filters, ref string sErr) {
/* Can not combine this             string sql = @"SELECT <%HList(<%PrimaryNotAccColumn(FieldName)%><%,%> )%>*/
            string sql = @"SELECT <%HList(<%PrimaryNotAccColumn(FieldName)%><%,%> )%>
            FROM  TheoDoiSX
            WHERE  IdSX = @IdSX AND XuongId = @XuongId";
			
			bool hasfilters = BeginFilterParameter(ref sql, filters);

            InitConnect();
            InitCommand(sql);
            AddParameter(TheoDoiSXInfo.Field.IdSX, IdSX);
            AddParameter(TheoDoiSXInfo.Field.XuongId, XuongId);
            if (hasfilters) AddFilterParameter(filters);
			
            DataTable list = new DataTable();
            try { list = executeSelect(); }
            catch (Exception ex) { sErr = string.Format("{0} sql='{1}'", ex.Message, sql); }

            return list;
        }
		
		public DataTable GetByFilterToDataTable(Int64 IdSX, String XuongId, List<DTO.FilterParameter> filters, ref string sErr, int indexPage = 0, int itemPerPage = 0) {            
            string sql = @"SELECT  IdSX, XuongId, LineId, ThoiGian, LoaiBang, TenChuyen, KhachHang, MaHang, SoLuongMaHang, DonGia, SoLaoDong, SoVang, DinhMuc1, DinhMuc2, DinhMuc3, DinhMuc4, DinhMuc5, HoanTat1, HoanTat2, HoanTat3, HoanTat4, HoanTat5, LuyKeSL1, LuyKeSL2, LuyKeSL3, LuyKeSL4, LuyKeSL5, KeHoachSL1, KeHoachSL2, KeHoachSL3, KeHoachSL4, WorkTimeOfDay, WorkTimeOfNow
            FROM  TheoDoiSX
            WHERE  IdSX = @IdSX AND XuongId = @XuongId";

            bool hasfilters = BeginFilterParameter(ref sql, filters);
			
			sql += @" ORDER BY  IdSX";
			
            if (itemPerPage != 0) {
                if (DbType == "S") sql += @" OFFSET @indexPage ROWS FETCH NEXT @itemPerPage ROWS ONLY";
                else sql += @" LIMIT @indexPage ,@itemPerPage ";
            }
            
            InitConnect();
            InitCommand(sql);
            AddParameter(TheoDoiSXInfo.Field.IdSX, IdSX);
            AddParameter(TheoDoiSXInfo.Field.XuongId, XuongId);
            if (itemPerPage != 0) {
                AddParameter("indexPage", indexPage);
                AddParameter("itemPerPage", itemPerPage);
            }

			if (hasfilters) AddFilterParameter(filters);
			
            DataTable list = new DataTable();
            try { list = executeSelect(); }
			catch (Exception ex) { sErr = string.Format("{0} sql='{1}'", ex.Message, sql); }

            return list;
        }
		
		
		public TheoDoiSXInfo[] GetByFilter(Int64 IdSX, String XuongId, List<DTO.FilterParameter> filters, ref string sErr, int indexPage = 0, int itemPerPage = 0) {
			DataTable list = GetByFilterToDataTable(IdSX, XuongId, filters, ref sErr, indexPage, itemPerPage);

			if (sErr != "") return null;
			
            TheoDoiSXInfo[] res = null;            
			
			try
			{
				res = new TheoDoiSXInfo[list.Rows.Count];
				for (int i = 0; i < list.Rows.Count; i++)
					res[i] = new TheoDoiSXInfo(list.Rows[i]);
			}
			catch (Exception ex) { sErr = ex.Message; }
            
			if (sErr != "") return null;
            return res;
        }
		
		public int GetCountRecord(Int64 IdSX, String XuongId, List<DTO.FilterParameter> filters, ref string sErr) {
            string sql = @"SELECT COUNT(*)
                FROM  TheoDoiSX
                WHERE  IdSX = @IdSX AND XuongId = @XuongId";
				
			bool hasfilters = BeginFilterParameter(ref sql, filters);
			
			int ret = -1;
            openConnection();
            InitCommand(sql);
            AddParameter(TheoDoiSXInfo.Field.IdSX, IdSX);
            AddParameter(TheoDoiSXInfo.Field.XuongId, XuongId);
          
			if (hasfilters) AddFilterParameter(filters);
		  
            try
            {
                object tmp = executeScalar();

                if(tmp != null && tmp != DBNull.Value) ret = Convert.ToInt32(tmp);
				else ret = 0;
            }
            catch (Exception ex) { sErr = string.Format("{0} sql='{1}'", ex.Message, sql); }
            finally { closeConnection(); }
			
            return ret;			
        }
		
		/// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public int Add(TheoDoiSXInfo objEntr, ref string sErr) {
            string sqlFields = @" INSERT INTO TheoDoiSX(IdSX,";
            string sqlValues = @" VALUES(@IdSX,";

            if (objEntr.XuongId != null) { sqlFields += " XuongId,"; sqlValues += " @XuongId,"; }
            if (objEntr.LineId != null) { sqlFields += " LineId,"; sqlValues += " @LineId,"; }
            if (objEntr.ThoiGian != null) { sqlFields += " ThoiGian,"; sqlValues += " @ThoiGian,"; }
            if (objEntr.LoaiBang != null) { sqlFields += " LoaiBang,"; sqlValues += " @LoaiBang,"; }
            if (objEntr.TenChuyen != null) { sqlFields += " TenChuyen,"; sqlValues += " @TenChuyen,"; }
            if (objEntr.KhachHang != null) { sqlFields += " KhachHang,"; sqlValues += " @KhachHang,"; }
            if (objEntr.MaHang != null) { sqlFields += " MaHang,"; sqlValues += " @MaHang,"; }
            if (objEntr.SoLuongMaHang != null) { sqlFields += " SoLuongMaHang,"; sqlValues += " @SoLuongMaHang,"; }
            if (objEntr.DonGia != null) { sqlFields += " DonGia,"; sqlValues += " @DonGia,"; }
            if (objEntr.SoLaoDong != null) { sqlFields += " SoLaoDong,"; sqlValues += " @SoLaoDong,"; }
            if (objEntr.SoVang != null) { sqlFields += " SoVang,"; sqlValues += " @SoVang,"; }
            if (objEntr.DinhMuc1 != null) { sqlFields += " DinhMuc1,"; sqlValues += " @DinhMuc1,"; }
            if (objEntr.DinhMuc2 != null) { sqlFields += " DinhMuc2,"; sqlValues += " @DinhMuc2,"; }
            if (objEntr.DinhMuc3 != null) { sqlFields += " DinhMuc3,"; sqlValues += " @DinhMuc3,"; }
            if (objEntr.DinhMuc4 != null) { sqlFields += " DinhMuc4,"; sqlValues += " @DinhMuc4,"; }
            if (objEntr.DinhMuc5 != null) { sqlFields += " DinhMuc5,"; sqlValues += " @DinhMuc5,"; }
            if (objEntr.HoanTat1 != null) { sqlFields += " HoanTat1,"; sqlValues += " @HoanTat1,"; }
            if (objEntr.HoanTat2 != null) { sqlFields += " HoanTat2,"; sqlValues += " @HoanTat2,"; }
            if (objEntr.HoanTat3 != null) { sqlFields += " HoanTat3,"; sqlValues += " @HoanTat3,"; }
            if (objEntr.HoanTat4 != null) { sqlFields += " HoanTat4,"; sqlValues += " @HoanTat4,"; }
            if (objEntr.HoanTat5 != null) { sqlFields += " HoanTat5,"; sqlValues += " @HoanTat5,"; }
            if (objEntr.LuyKeSL1 != null) { sqlFields += " LuyKeSL1,"; sqlValues += " @LuyKeSL1,"; }
            if (objEntr.LuyKeSL2 != null) { sqlFields += " LuyKeSL2,"; sqlValues += " @LuyKeSL2,"; }
            if (objEntr.LuyKeSL3 != null) { sqlFields += " LuyKeSL3,"; sqlValues += " @LuyKeSL3,"; }
            if (objEntr.LuyKeSL4 != null) { sqlFields += " LuyKeSL4,"; sqlValues += " @LuyKeSL4,"; }
            if (objEntr.LuyKeSL5 != null) { sqlFields += " LuyKeSL5,"; sqlValues += " @LuyKeSL5,"; }
            if (objEntr.KeHoachSL1 != null) { sqlFields += " KeHoachSL1,"; sqlValues += " @KeHoachSL1,"; }
            if (objEntr.KeHoachSL2 != null) { sqlFields += " KeHoachSL2,"; sqlValues += " @KeHoachSL2,"; }
            if (objEntr.KeHoachSL3 != null) { sqlFields += " KeHoachSL3,"; sqlValues += " @KeHoachSL3,"; }
            if (objEntr.KeHoachSL4 != null) { sqlFields += " KeHoachSL4,"; sqlValues += " @KeHoachSL4,"; }
            if (objEntr.WorkTimeOfDay != null) { sqlFields += " WorkTimeOfDay,"; sqlValues += " @WorkTimeOfDay,"; }
            if (objEntr.WorkTimeOfNow != null) { sqlFields += " WorkTimeOfNow,"; sqlValues += " @WorkTimeOfNow,"; }
/* Can not combine this 			if (objEntr.<%CreateColumn(Name)%> != null) { sqlFields += " <%CreateColumn(FieldName)%>,"; sqlValues += " @<%CreateColumn(FieldName)%>,"; }*/
/* Can not combine this 			if (objEntr.<%UpdateColumn(Name)%> != null) { sqlFields += " <%UpdateColumn(FieldName)%>,"; sqlValues += " @<%UpdateColumn(FieldName)%>,"; }*/

            sqlFields = sqlFields.Substring(0, sqlFields.Length - 1) + ")";
            sqlValues = sqlValues.Substring(0, sqlValues.Length - 1) + ")";

            int ret = -1;
			string sql = sqlFields + sqlValues;
            openConnection();
            InitCommand(sql);
            AddParameter(TheoDoiSXInfo.Field.IdSX, objEntr.IdSX);

            if (objEntr.XuongId != null) AddParameter(TheoDoiSXInfo.Field.XuongId, objEntr.XuongId);         
            if (objEntr.LineId != null) AddParameter(TheoDoiSXInfo.Field.LineId, objEntr.LineId);         
            if (objEntr.ThoiGian != null) AddParameter(TheoDoiSXInfo.Field.ThoiGian, objEntr.ThoiGian);         
            if (objEntr.LoaiBang != null) AddParameter(TheoDoiSXInfo.Field.LoaiBang, objEntr.LoaiBang);         
            if (objEntr.TenChuyen != null) AddParameter(TheoDoiSXInfo.Field.TenChuyen, objEntr.TenChuyen);         
            if (objEntr.KhachHang != null) AddParameter(TheoDoiSXInfo.Field.KhachHang, objEntr.KhachHang);         
            if (objEntr.MaHang != null) AddParameter(TheoDoiSXInfo.Field.MaHang, objEntr.MaHang);         
            if (objEntr.SoLuongMaHang != null) AddParameter(TheoDoiSXInfo.Field.SoLuongMaHang, objEntr.SoLuongMaHang);         
            if (objEntr.DonGia != null) AddParameter(TheoDoiSXInfo.Field.DonGia, objEntr.DonGia);         
            if (objEntr.SoLaoDong != null) AddParameter(TheoDoiSXInfo.Field.SoLaoDong, objEntr.SoLaoDong);         
            if (objEntr.SoVang != null) AddParameter(TheoDoiSXInfo.Field.SoVang, objEntr.SoVang);         
            if (objEntr.DinhMuc1 != null) AddParameter(TheoDoiSXInfo.Field.DinhMuc1, objEntr.DinhMuc1);         
            if (objEntr.DinhMuc2 != null) AddParameter(TheoDoiSXInfo.Field.DinhMuc2, objEntr.DinhMuc2);         
            if (objEntr.DinhMuc3 != null) AddParameter(TheoDoiSXInfo.Field.DinhMuc3, objEntr.DinhMuc3);         
            if (objEntr.DinhMuc4 != null) AddParameter(TheoDoiSXInfo.Field.DinhMuc4, objEntr.DinhMuc4);         
            if (objEntr.DinhMuc5 != null) AddParameter(TheoDoiSXInfo.Field.DinhMuc5, objEntr.DinhMuc5);         
            if (objEntr.HoanTat1 != null) AddParameter(TheoDoiSXInfo.Field.HoanTat1, objEntr.HoanTat1);         
            if (objEntr.HoanTat2 != null) AddParameter(TheoDoiSXInfo.Field.HoanTat2, objEntr.HoanTat2);         
            if (objEntr.HoanTat3 != null) AddParameter(TheoDoiSXInfo.Field.HoanTat3, objEntr.HoanTat3);         
            if (objEntr.HoanTat4 != null) AddParameter(TheoDoiSXInfo.Field.HoanTat4, objEntr.HoanTat4);         
            if (objEntr.HoanTat5 != null) AddParameter(TheoDoiSXInfo.Field.HoanTat5, objEntr.HoanTat5);         
            if (objEntr.LuyKeSL1 != null) AddParameter(TheoDoiSXInfo.Field.LuyKeSL1, objEntr.LuyKeSL1);         
            if (objEntr.LuyKeSL2 != null) AddParameter(TheoDoiSXInfo.Field.LuyKeSL2, objEntr.LuyKeSL2);         
            if (objEntr.LuyKeSL3 != null) AddParameter(TheoDoiSXInfo.Field.LuyKeSL3, objEntr.LuyKeSL3);         
            if (objEntr.LuyKeSL4 != null) AddParameter(TheoDoiSXInfo.Field.LuyKeSL4, objEntr.LuyKeSL4);         
            if (objEntr.LuyKeSL5 != null) AddParameter(TheoDoiSXInfo.Field.LuyKeSL5, objEntr.LuyKeSL5);         
            if (objEntr.KeHoachSL1 != null) AddParameter(TheoDoiSXInfo.Field.KeHoachSL1, objEntr.KeHoachSL1);         
            if (objEntr.KeHoachSL2 != null) AddParameter(TheoDoiSXInfo.Field.KeHoachSL2, objEntr.KeHoachSL2);         
            if (objEntr.KeHoachSL3 != null) AddParameter(TheoDoiSXInfo.Field.KeHoachSL3, objEntr.KeHoachSL3);         
            if (objEntr.KeHoachSL4 != null) AddParameter(TheoDoiSXInfo.Field.KeHoachSL4, objEntr.KeHoachSL4);         
            if (objEntr.WorkTimeOfDay != null) AddParameter(TheoDoiSXInfo.Field.WorkTimeOfDay, objEntr.WorkTimeOfDay);         
            if (objEntr.WorkTimeOfNow != null) AddParameter(TheoDoiSXInfo.Field.WorkTimeOfNow, objEntr.WorkTimeOfNow);         
/* Can not combine this 			if (objEntr.<%CreateColumn(Name)%> != null) AddParameter(TheoDoiSXInfo.Field.<%CreateColumn(Name)%>, objEntr.<%CreateColumn(Name)%>);*/
/* Can not combine this 			if (objEntr.<%UpdateColumn(Name)%> != null) AddParameter(TheoDoiSXInfo.Field.<%UpdateColumn(Name)%>, objEntr.<%UpdateColumn(Name)%>);*/
          
            try
            {
                object tmp = executeScalar();
                if(tmp != null && tmp != DBNull.Value) ret = Convert.ToInt32(tmp);
				else ret = 0;
            }
            catch (Exception ex) { sErr = string.Format("{0} sql='{1}'", ex.Message, sql); }
            finally { closeConnection(); }

            return ret;
        }

        public string Update(TheoDoiSXInfo objEntr) {
            string sql = @"UPDATE TheoDoiSX SET ";
			
            if (objEntr.XuongId != null) { sql += "XuongId = @XuongId,"; }
            if (objEntr.LineId != null) { sql += "LineId = @LineId,"; }
            if (objEntr.ThoiGian != null) { sql += "ThoiGian = @ThoiGian,"; }
            if (objEntr.LoaiBang != null) { sql += "LoaiBang = @LoaiBang,"; }
            if (objEntr.TenChuyen != null) { sql += "TenChuyen = @TenChuyen,"; }
            if (objEntr.KhachHang != null) { sql += "KhachHang = @KhachHang,"; }
            if (objEntr.MaHang != null) { sql += "MaHang = @MaHang,"; }
            if (objEntr.SoLuongMaHang != null) { sql += "SoLuongMaHang = @SoLuongMaHang,"; }
            if (objEntr.DonGia != null) { sql += "DonGia = @DonGia,"; }
            if (objEntr.SoLaoDong != null) { sql += "SoLaoDong = @SoLaoDong,"; }
            if (objEntr.SoVang != null) { sql += "SoVang = @SoVang,"; }
            if (objEntr.DinhMuc1 != null) { sql += "DinhMuc1 = @DinhMuc1,"; }
            if (objEntr.DinhMuc2 != null) { sql += "DinhMuc2 = @DinhMuc2,"; }
            if (objEntr.DinhMuc3 != null) { sql += "DinhMuc3 = @DinhMuc3,"; }
            if (objEntr.DinhMuc4 != null) { sql += "DinhMuc4 = @DinhMuc4,"; }
            if (objEntr.DinhMuc5 != null) { sql += "DinhMuc5 = @DinhMuc5,"; }
            if (objEntr.HoanTat1 != null) { sql += "HoanTat1 = @HoanTat1,"; }
            if (objEntr.HoanTat2 != null) { sql += "HoanTat2 = @HoanTat2,"; }
            if (objEntr.HoanTat3 != null) { sql += "HoanTat3 = @HoanTat3,"; }
            if (objEntr.HoanTat4 != null) { sql += "HoanTat4 = @HoanTat4,"; }
            if (objEntr.HoanTat5 != null) { sql += "HoanTat5 = @HoanTat5,"; }
            if (objEntr.LuyKeSL1 != null) { sql += "LuyKeSL1 = @LuyKeSL1,"; }
            if (objEntr.LuyKeSL2 != null) { sql += "LuyKeSL2 = @LuyKeSL2,"; }
            if (objEntr.LuyKeSL3 != null) { sql += "LuyKeSL3 = @LuyKeSL3,"; }
            if (objEntr.LuyKeSL4 != null) { sql += "LuyKeSL4 = @LuyKeSL4,"; }
            if (objEntr.LuyKeSL5 != null) { sql += "LuyKeSL5 = @LuyKeSL5,"; }
            if (objEntr.KeHoachSL1 != null) { sql += "KeHoachSL1 = @KeHoachSL1,"; }
            if (objEntr.KeHoachSL2 != null) { sql += "KeHoachSL2 = @KeHoachSL2,"; }
            if (objEntr.KeHoachSL3 != null) { sql += "KeHoachSL3 = @KeHoachSL3,"; }
            if (objEntr.KeHoachSL4 != null) { sql += "KeHoachSL4 = @KeHoachSL4,"; }
            if (objEntr.WorkTimeOfDay != null) { sql += "WorkTimeOfDay = @WorkTimeOfDay,"; }
            if (objEntr.WorkTimeOfNow != null) { sql += "WorkTimeOfNow = @WorkTimeOfNow,"; }
/* Can not combine this 			if (objEntr.<%UpdateColumn(Name)%> != null) { sql += "<%UpdateColumn(FieldName)%> = @<%UpdateColumn(FieldName)%>,"; }*/

			sql += " lastupdate = getdate()";
            sql += @" WHERE  IdSX = @IdSX";

            openConnection();
            InitCommand(sql);

            AddParameter(TheoDoiSXInfo.Field.IdSX, objEntr.IdSX);

            if (objEntr.XuongId != null) AddParameter(TheoDoiSXInfo.Field.XuongId, objEntr.XuongId);         
            if (objEntr.LineId != null) AddParameter(TheoDoiSXInfo.Field.LineId, objEntr.LineId);         
            if (objEntr.ThoiGian != null) AddParameter(TheoDoiSXInfo.Field.ThoiGian, objEntr.ThoiGian);         
            if (objEntr.LoaiBang != null) AddParameter(TheoDoiSXInfo.Field.LoaiBang, objEntr.LoaiBang);         
            if (objEntr.TenChuyen != null) AddParameter(TheoDoiSXInfo.Field.TenChuyen, objEntr.TenChuyen);         
            if (objEntr.KhachHang != null) AddParameter(TheoDoiSXInfo.Field.KhachHang, objEntr.KhachHang);         
            if (objEntr.MaHang != null) AddParameter(TheoDoiSXInfo.Field.MaHang, objEntr.MaHang);         
            if (objEntr.SoLuongMaHang != null) AddParameter(TheoDoiSXInfo.Field.SoLuongMaHang, objEntr.SoLuongMaHang);         
            if (objEntr.DonGia != null) AddParameter(TheoDoiSXInfo.Field.DonGia, objEntr.DonGia);         
            if (objEntr.SoLaoDong != null) AddParameter(TheoDoiSXInfo.Field.SoLaoDong, objEntr.SoLaoDong);         
            if (objEntr.SoVang != null) AddParameter(TheoDoiSXInfo.Field.SoVang, objEntr.SoVang);         
            if (objEntr.DinhMuc1 != null) AddParameter(TheoDoiSXInfo.Field.DinhMuc1, objEntr.DinhMuc1);         
            if (objEntr.DinhMuc2 != null) AddParameter(TheoDoiSXInfo.Field.DinhMuc2, objEntr.DinhMuc2);         
            if (objEntr.DinhMuc3 != null) AddParameter(TheoDoiSXInfo.Field.DinhMuc3, objEntr.DinhMuc3);         
            if (objEntr.DinhMuc4 != null) AddParameter(TheoDoiSXInfo.Field.DinhMuc4, objEntr.DinhMuc4);         
            if (objEntr.DinhMuc5 != null) AddParameter(TheoDoiSXInfo.Field.DinhMuc5, objEntr.DinhMuc5);         
            if (objEntr.HoanTat1 != null) AddParameter(TheoDoiSXInfo.Field.HoanTat1, objEntr.HoanTat1);         
            if (objEntr.HoanTat2 != null) AddParameter(TheoDoiSXInfo.Field.HoanTat2, objEntr.HoanTat2);         
            if (objEntr.HoanTat3 != null) AddParameter(TheoDoiSXInfo.Field.HoanTat3, objEntr.HoanTat3);         
            if (objEntr.HoanTat4 != null) AddParameter(TheoDoiSXInfo.Field.HoanTat4, objEntr.HoanTat4);         
            if (objEntr.HoanTat5 != null) AddParameter(TheoDoiSXInfo.Field.HoanTat5, objEntr.HoanTat5);         
            if (objEntr.LuyKeSL1 != null) AddParameter(TheoDoiSXInfo.Field.LuyKeSL1, objEntr.LuyKeSL1);         
            if (objEntr.LuyKeSL2 != null) AddParameter(TheoDoiSXInfo.Field.LuyKeSL2, objEntr.LuyKeSL2);         
            if (objEntr.LuyKeSL3 != null) AddParameter(TheoDoiSXInfo.Field.LuyKeSL3, objEntr.LuyKeSL3);         
            if (objEntr.LuyKeSL4 != null) AddParameter(TheoDoiSXInfo.Field.LuyKeSL4, objEntr.LuyKeSL4);         
            if (objEntr.LuyKeSL5 != null) AddParameter(TheoDoiSXInfo.Field.LuyKeSL5, objEntr.LuyKeSL5);         
            if (objEntr.KeHoachSL1 != null) AddParameter(TheoDoiSXInfo.Field.KeHoachSL1, objEntr.KeHoachSL1);         
            if (objEntr.KeHoachSL2 != null) AddParameter(TheoDoiSXInfo.Field.KeHoachSL2, objEntr.KeHoachSL2);         
            if (objEntr.KeHoachSL3 != null) AddParameter(TheoDoiSXInfo.Field.KeHoachSL3, objEntr.KeHoachSL3);         
            if (objEntr.KeHoachSL4 != null) AddParameter(TheoDoiSXInfo.Field.KeHoachSL4, objEntr.KeHoachSL4);         
            if (objEntr.WorkTimeOfDay != null) AddParameter(TheoDoiSXInfo.Field.WorkTimeOfDay, objEntr.WorkTimeOfDay);         
            if (objEntr.WorkTimeOfNow != null) AddParameter(TheoDoiSXInfo.Field.WorkTimeOfNow, objEntr.WorkTimeOfNow);         
/* Can not combine this 			if (objEntr.<%UpdateColumn(Name)%> != null) AddParameter(TheoDoiSXInfo.Field.<%UpdateColumn(Name)%>, objEntr.<%UpdateColumn(Name)%>);          */
               
            string sErr = "";

            try { executeNonQuery(); }
            catch (Exception ex) { sErr = string.Format("{0} sql='{1}'", ex.Message, sql); }
            finally { closeConnection(); }

            return sErr;
        }

        public string Delete(Int64 IdSX) {
            string sql = @"DELETE TheoDoiSX
            WHERE 	 IdSX = @IdSX";

            openConnection();
            InitCommand(sql);

            AddParameter(TheoDoiSXInfo.Field.IdSX, IdSX);
              
            string sErr = "";
            try { executeNonQuery(); }
            catch (Exception ex) { sErr = string.Format("{0} sql='{1}'", ex.Message, sql); }
            finally { closeConnection(); }

            return sErr;
        }   	
        
        public Boolean IsExist(Int64? IdSX, ref string sErr) {
            string sql = @"SELECT TOP (1) IdSX
            FROM  TheoDoiSX
            WHERE 	 IdSX = @IdSX";
			
            InitConnect();
            InitCommand(sql);
            AddParameter(TheoDoiSXInfo.Field.IdSX, IdSX);              
            
            DataTable list = new DataTable();
            try { list = executeSelect(); }
            catch (Exception ex) { sErr = ex.Message + " sql='" + sql + "'"; }

			return list.Rows.Count == 1;
        }		
		
		#endregion Method
     
    }
}
